export default {
    install(Vue) {

        /**
         * 注销操作,只为了保存注销记录,不需要反馈
         */
        Vue.prototype.standLoginOut = function () {
            this.requestPost('/class_api/user/logout', null, function () {

            });
        };

        // 格式化处理列表数据
        Vue.prototype.formatListData = function (response) {
            const result = {
                list: [],
                total: 0
            };
            const body = response.body;
            if (!body) {
                return result;
            }
            if (!body.success) {
                return result;
            }
            if (!body.data) {
                return result;
            }
            if (body.data.content.length == 0) {
                return result;
            }
            // 设置参数
            result.list = body.data.content;
            result.total = body.data.totalElements;
            return result;
        };

        // 格式化普通数据
        Vue.prototype.formatNormalData = function (response) {
            const result = {
                success: false,
                data: {},
                errorMsg: '系统内部错误,请联系管理员',
                errorCode: '1001'
            };
            const body = response.body;
            if (!body) {
                return result;
            }
            if (!body.success) {
                result.errorMsg = body.errorMsg;
                result.errorCode = body.errorCode;
                return result;
            }
            result.success = true;
            result.errorMsg = body.errorMsg;
            result.data = body.data;
            result.errorCode = body.errorCode;
            if (!result.success) {
                result.success = false;
            }
            if (!result.errorMsg) {
                result.errorMsg = '系统内部错误,请联系管理员';
            }
            if (!result.errorCode) {
                result.errorCode = '1001';
            }
            if (!result.data) {
                result.data = {};
            }
            return result;
        };

        /**
         * 标准的获取列表的post请求
         * @param url
         * @param data
         * @param callback
         */
        Vue.prototype.requestList = function (url, data, callBack) {
            this.$http.post(url, data).then(response => {
                const result = this.formatListData(response);
                callBack(result.total, result.list);
            }, response => {
                callBack(0, []);
            });
        };


        /**
         * 标准的put请求操作,返回的是 success , data , errorMsg
         * @param url
         * @param data
         * @returns {Promise<unknown>}
         */
        Vue.prototype.requestPost = function (url, data, callBack) {
            console.log('请求方式:requestPost');
            this.$http.post(url, data).then(response => {
                const result = this.formatNormalData(response);
                console.log('返回结果:', result);
                callBack(result);
            }, response => {
                callBack({
                    success: false,
                    data: {},
                    errorMsg: '系统内部错误,请联系管理员',
                    errorCode: '1001'
                })
                ;
            });
        };

        /**
         * 标准的put请求操作,返回的是 success , data , errorMsg
         * @param url
         * @param data
         * @returns {Promise<unknown>}
         */
        Vue.prototype.requestDelete = function (url, callBack) {
            console.log('请求方式:requestDelete');
            this.$http.delete(url).then(response => {
                const result = this.formatNormalData(response);
                console.log('返回结果:', result);
                callBack(result);
            }, response => {
                callBack({
                    success: false,
                    data: {},
                    errorMsg: '系统内部错误,请联系管理员',
                    errorCode: '1001'
                })
                ;
            });
        };
        /**
         * 标准的put请求操作,返回的是 success , data , errorMsg
         * @param url
         * @param data
         * @returns {Promise<unknown>}
         */
        Vue.prototype.requestPut = function (url, data, callBack) {
            console.log('请求方式:requestPut');
            this.$http.put(url, data).then(response => {
                const result = this.formatNormalData(response);
                console.log('返回结果:', result);
                callBack(result);
            }, response => {
                callBack({
                    success: false,
                    data: {},
                    errorMsg: '系统内部错误,请联系管理员',
                    errorCode: '1001'
                })
                ;
            });
        };

        /**
         * 标准的put请求操作,返回的是 success , data , errorMsg
         * @param url
         * @param data
         * @returns {Promise<unknown>}
         */
        Vue.prototype.requestGet = function (url,  callBack) {
            console.log('请求方式:requestGet');
            this.$http.get(url).then(response => {
                const result = this.formatNormalData(response);
                console.log('返回结果:', result);
                callBack(result);
            }, response => {
                callBack({
                    success: false,
                    data: {},
                    errorMsg: '系统内部错误,请联系管理员',
                    errorCode: '1001'
                })
                ;
            });
        };

        /**
         * 标准的打印文件方法
         * @param url
         * @param data
         * @returns {Promise<unknown>}
         */
        Vue.prototype.requestFile = function (url, data) {
            console.log('请求方式:requestFile');
            this.showFullPageLoading();
            this.$http.post(url, data).then(response => {
                this.hideFullLoading();
                const result = this.formatNormalData(response);
                console.log('返回结果:', result);
                if (!result.success || result.data == null || this.strError(result.data.path)) {
                    this.showError('打印文件失败,请稍后重试');
                    return;
                }
                // 打开pdf
                this.downloadFile(result.data.path);
            }, response => {
                this.hideFullLoading();
                this.showError('打印文件失败,请稍后重试');
            });
        };


        /**
         * 标准的delete请求操作,返回的是 success , data , errorMsg
         * @param url
         * @param data
         * @returns {Promise<unknown>}
         */
        Vue.prototype.downloadFile = function (url) {
            const path = '/class_api/file/' + url;
            window.open(path);
        };
        /**
         * 标准的delete请求操作,返回的是 success , data , errorMsg
         * @param url
         * @param data
         * @returns {Promise<unknown>}
         */
        Vue.prototype.getFileUrl = function (url) {
            if (!url) {
                return '';
            }
            const path = '/class_api/file/' + url;
            return path;
        };

        /**
         * 标准的打印pdf文件的方法
         * @param url
         * @param data
         * @param callBack
         */
        Vue.prototype.standPrintFile = function (url, data, callBack) {
            this.$http.post(url, data).then(response => {
                var result = response.body;
                if (result == null) {
                    this.showError('打印文件失败,请稍后重试');
                    callBack();
                    return;
                }
                if (!result.success || result.data == null || this.strError(result.data.path)) {
                    this.showError('打印文件失败,请稍后重试');
                    callBack();
                    return;
                }
                // 打开pdf
                this.downloadFile(result.data.path);
                callBack();
            }, response => {
                this.showError('打印文件失败,请稍后重试');
                callBack();
            });
        };

    }
};
